home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue30 / construc / DRBOBCGI.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-01-04  |  2.4 KB  |  113 lines

  1. unit DrBobCGI;
  2. {$I-}
  3. interface
  4. type
  5.   TCGIprotocol = (CGI,WinCGI);
  6. var
  7.   CGIProtocol: TCGIprotocol = CGI;
  8.  
  9. type
  10.   TRequestMethod = (Unknown,Get,Post);
  11. var
  12.   RequestMethod: TRequestMethod = Unknown;
  13.  
  14. var
  15.   ContentLength: Integer = 0;
  16.   Data: AnsiString = '';
  17.  
  18.   function Value(const Field: ShortString): ShortString;
  19.  
  20.   function ValueAsInteger(const Field: ShortString): Integer;
  21.  
  22. implementation
  23. uses
  24.   DrBobSys;
  25.  
  26.   function Value(const Field: ShortString): ShortString;
  27.   { 1998/01/02: check for complete match of Field name }
  28.   var
  29.     i: Integer;
  30.     len: Byte absolute Result;
  31.   begin
  32.     Len := 0;
  33.     i := Pos('&'+Field+'=',Data);
  34.     if i = 0 then
  35.     begin
  36.       i := Pos(Field+'=',Data);
  37.       if i > 1 then i := 0
  38.     end
  39.     else Inc(i); { skip '&' }
  40.     if i > 0 then
  41.     begin
  42.       Inc(i,Length(Field)+1);
  43.       while Data[i] <> '&' do
  44.       begin
  45.         Inc(Len);
  46.         Result[Len] := Data[i];
  47.         Inc(i)
  48.       end
  49.     end
  50.   end {Value};
  51.  
  52.   function ValueAsInteger(const Field: ShortString): Integer;
  53.   begin
  54.     Result := StrToInt(Value(Field))
  55.   end {ValueAsInteger};
  56.  
  57. var
  58.   P: PChar;
  59.   i: Integer;
  60.   Str: ShortString;
  61.  
  62. initialization
  63.   P := GetEnvironmentStrings;
  64.   while P^ <> #0 do
  65.   begin
  66.     Str := StrPas(P);
  67.     if Pos('REQUEST_METHOD=',Str) > 0 then
  68.     begin
  69.       Delete(Str,1,Pos('=',Str));
  70.       if Str = 'POST' then RequestMethod := Post
  71.       else
  72.         if Str = 'GET' then RequestMethod := Get
  73.     end;
  74.     if Pos('CONTENT_LENGTH=',Str) = 1 then
  75.     begin
  76.       Delete(Str,1,Pos('=',Str));
  77.       ContentLength := StrToInt(Str)
  78.     end;
  79.     if Pos('QUERY_STRING=',Str) > 0 then
  80.     begin
  81.       Delete(Str,1,Pos('=',Str));
  82.       SetLength(Data,Length(Str)+1);
  83.       Data := Str
  84.     end;
  85.     Inc(P, StrLen(P)+1)
  86.   end;
  87.   if RequestMethod = Post then
  88.   begin
  89.     SetLength(Data,ContentLength+1);
  90.     for i:=1 to ContentLength do read(Data[i]);
  91.     Data[ContentLength+1] := '&';
  92.   { if IOResult <> 0 then { skip }
  93.   end;
  94.   i := 0;
  95.   while i < Length(Data) do
  96.   begin
  97.     Inc(i);
  98.     if Data[i] = '+' then Data[i] := ' ';
  99.     if Data[i] = '%' then { special code }
  100.     begin
  101.       Str := '$00';
  102.       Str[2] := Data[i+1];
  103.       Str[3] := Data[i+2];
  104.       Delete(Data,i+1,2);
  105.       Data[i] := Chr(StrToInt(Str))
  106.     end
  107.   end;
  108.   if i > 0 then Data[i+1] := '&'
  109.            else Data := '&'
  110. finalization
  111.   Data := ''
  112. end.
  113.